package com.goldsprite.gameframeworks.ruletilemap.core;

import java.util.Map;

public class MapRenderer extends BaseMapRenderer {

	public MapRenderer() {
		this.tileSize = 1; // 控制台渲染不需要像素尺寸
	}

	@Override
	public void initializeIcons(Map<String, Object> iconMap) {
		this.iconMapping.clear();
		this.iconMapping.putAll(iconMap);
	}

	public void initializeDefaultIcons(String ids, String icons) {
		String[] idArray = ids.split(" ");
		String[] iconArray = icons.split(" ");

		if (idArray.length != iconArray.length) {
			throw new IllegalArgumentException("IDs和图标数量不匹配");
		}

		for (int i = 0; i < idArray.length; i++) {
			iconMapping.put(idArray[i], iconArray[i]);
		}
	}

	@Override
	public void renderMap(String mapData) {
		String[][] grid = parseMapToGrid(mapData);
		for (String[] row : grid) {
			for (String cell : row) {
				String icon = (String) iconMapping.getOrDefault(cell, "?");
				System.out.print(icon + " ");
			}
			System.out.println();
		}
	}

	@Override
	public void dispose() {
		// 控制台渲染器无需释放资源
		iconMapping.clear();
	}

	public void addTileIcon(String tileId, String icon) {
		iconMapping.put(tileId, icon);
	}
}
